package com.jy.unkown;

import android.text.TextUtils;

import androidx.annotation.Keep;

import com.google.gson.Gson;

import com.jy.unkown.entity.CsdnIpBean;
import com.jy.unkown.entity.IpChaxunBean;
import com.jy.unkown.entity.IpPlus360Bean;
import com.jy.unkown.entity.WhoisBean;


import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

@Keep
public class IPUtils {
    static String ip;

    public static String getIp() {
        return ip;
    }

    public static void start() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                ip();
            }
        }).start();
    }


    private static void ip() {
        try {
            {
                String url = "https://searchplugin.csdn.net/api/v1/ip/get";
                String result = getIp(url);
                if (!TextUtils.isEmpty(result)) {
                    CsdnIpBean csdnIpBean = new Gson().fromJson(result, CsdnIpBean.class);
                    if (csdnIpBean != null && csdnIpBean.code == 200 && csdnIpBean.data != null) {
                        ip = csdnIpBean.data.ip;
                    }
                }
            }

            if (TextUtils.isEmpty(ip)) {
                String url = "https://www.ipplus360.com/getIP";
                String result = getIp(url);
                if (!TextUtils.isEmpty(result)) {
                    IpPlus360Bean ipPlus360Bean = new Gson().fromJson(result, IpPlus360Bean.class);
                    if (ipPlus360Bean != null && ipPlus360Bean.code == 200) {
                        ip = ipPlus360Bean.data;
                    }
                }
            }
            if (TextUtils.isEmpty(ip)) {
                String url = "https://2023.ipchaxun.com/";
                String result = getIp(url);
                if (!TextUtils.isEmpty(result)) {
                    IpChaxunBean ipChaxunBean = new Gson().fromJson(result, IpChaxunBean.class);
                    if (ipChaxunBean != null && TextUtils.equals(ipChaxunBean.ret, "ok")) {
                        ip = ipChaxunBean.ip;
                    }
                }
            }
            if (TextUtils.isEmpty(ip)) {
                String url = "https://whois.pconline.com.cn/ipJson.jsp";
                String result = getIp(url);
                if (!TextUtils.isEmpty(result)) {
                    String json = result.replace("if(window.IPCallBack) {IPCallBack(", "").replace(");}", "");
                    WhoisBean whoisBean = new Gson().fromJson(json, WhoisBean.class);
                    if (whoisBean != null && TextUtils.equals(whoisBean.regionCode, "0")) {
                        ip = whoisBean.ip;
                    }
                }
            }
        } catch (Exception exception) {

        }

    }


     static String getIp(String url) {
        try {
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(url)//请求接口，如果需要传参拼接到接口后面
                    .removeHeader("User-Agent")
                    .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36")
                    .build(); //创建Request对象
            Response response = null;

            response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                String json = response.body().string();
                return json;
            }
        } catch (Exception exception) {

        }
        return null;
    }


}
